package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import javax.crypto.spec.IvParameterSpec;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class FreakyChatChildListAdapter extends BaseAdapter {
	// private DoctorTalkChildListActivity activity;
	private Context activity;
	private ArrayList<CatalogBean> lists;
	private BitmapUtils bitmapUtil;
	private int gridview_item_image_height = 0;

	public FreakyChatChildListAdapter(Context activity, BitmapUtils bitmapUtil,
			int gridview_item_image_height) {
		this.activity = activity;
		this.bitmapUtil = bitmapUtil;
		this.gridview_item_image_height = gridview_item_image_height;
	}

	public void setList(ArrayList<CatalogBean> beans) {
		if (lists == null) {
			lists = new ArrayList<CatalogBean>();
		}
		lists.addAll(beans);
	}

	@Override
	public int getCount() {
		return lists != null && lists.size() > 0 ? lists.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return lists != null && lists.size() > 0 ? lists.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHold hold = null;
		if (convertView == null) {
			hold = new ViewHold();
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.freaky_chat_child_item_view, null);
			hold.iv_iamge = (ImageView) convertView
					.findViewById(R.id.iv_child_icon);
			LayoutParams params = (LayoutParams) hold.iv_iamge
					.getLayoutParams();
			params.width = LayoutParams.FILL_PARENT;
			params.height = gridview_item_image_height;
			hold.iv_iamge.setLayoutParams(params);

			hold.tv_chat_content = (TextView) convertView
					.findViewById(R.id.tv_chat_content);
			hold.iv_freaky_icon = (ImageView) convertView
					.findViewById(R.id.iv_freaky_icon);
			hold.tv_freaky_name = (TextView) convertView
					.findViewById(R.id.tv_freaky_name);
			hold.tv_chat_like_count = (TextView) convertView
					.findViewById(R.id.tv_chat_like_count);
			hold.tv_chat_reply_count = (TextView) convertView
					.findViewById(R.id.tv_chat_reply_count);
			convertView.setTag(hold);
		} else {
			hold = (ViewHold) convertView.getTag();
		}

		if (lists == null)
			return null;
		final CatalogBean obj = lists.get(position);
		CatalogBean catalog = (CatalogBean) obj;
		bindCatalogImageView(hold, catalog);// 绑定专题图片
		hold.tv_chat_content.setText(catalog.getTitle()); // 专题标题
		
		if (catalog.getCatalogItems() != null
				&& catalog.getCatalogItems().size() > 0) {
			//if (catalog.getCatalog_type() == 1) { // 3=问题和1=话题
				CatalogItemBean item = catalog.getCatalogItems().get(0);
				if (item != null && !item.equals("{}")) {
					CatalogTopicItem topic = (CatalogTopicItem) item;
					if (topic.getItem() != null && !topic.getItem().equals("{}")) {
						//bindImageView(hold, topic);// 绑定图片
						//hold.tv_chat_content.setText(topic.getItem().getTitle()); // 话题内容
						bindFreakyIconView(hold, topic);// 绑定用户头像
						if (topic.getItem().getPost_actor() != null)
							hold.tv_freaky_name.setText(topic.getItem()
									.getPost_actor().getDisplay_name()
									+ "");// 昵称

						hold.tv_chat_like_count.setText(topic.getItem()
								.getLikes_count() + "");// 赞数
						hold.tv_chat_reply_count.setText(topic.getItem()
								.getReply_count() + ""); // 回复数

					}
				}
			//}
		}else{
			//hold.tv_chat_like_count.setText( "0");// 赞数
			//hold.tv_chat_reply_count.setText("0"); // 回复数
		}
		return convertView;
	}

	private void bindImageView(ViewHold hold, CatalogTopicItem topic) {
		if (topic.getItem().getFile_metas() != null) {
			String url = topic.getItem().getFile_metas().get(0)
					.getDownload_urls().getBig().getUrl();
			hold.iv_iamge.setImageDrawable(null);
			bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
			bitmapUtil.display(hold.iv_iamge, url);
		} else {
			hold.iv_iamge.setImageDrawable(null);
			hold.iv_iamge.setImageResource(R.drawable.iask_main_middle_img);

		}
	}
	private void bindCatalogImageView(ViewHold hold, CatalogBean catalog) {
		if (catalog.getFile_meta() != null) {
			String url = catalog.getFile_meta().getDownload_urls().getBig().getUrl();
			hold.iv_iamge.setImageDrawable(null);
			bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
			 bitmapUtil.configDefaultLoadFailedImage(R.drawable.iask_main_middle_img);
			bitmapUtil.display(hold.iv_iamge, url);
		} else {
			hold.iv_iamge.setImageDrawable(null);
			hold.iv_iamge.setImageResource(R.drawable.iask_main_middle_img);

		}
	}

	private void bindFreakyIconView(ViewHold hold, CatalogTopicItem topic) {
		if (topic.getItem() != null) {
			if (topic.getItem().getPost_actor() != null) {
				if (topic.getItem().getPost_actor().getAvatar_file_meta() != null) {
					String url = topic.getItem().getPost_actor()
							.getAvatar_file_meta().getDownload_urls().getSmal()
							.getUrl();

					hold.iv_freaky_icon.setImageDrawable(null);
					bitmapUtil.display(hold.iv_freaky_icon, url);
				}

			} else {
				hold.iv_iamge.setImageDrawable(null);
				hold.iv_iamge.setImageResource(R.drawable.iask_main_middle_img);

			}
		}
	}

	class ViewHold {
		ImageView iv_iamge;
		TextView tv_chat_content;
		ImageView iv_freaky_icon;
		TextView tv_freaky_name;
		TextView tv_chat_like_count;
		TextView tv_chat_reply_count;
	}

}
